package com.ihunanren.dao.ibatis.weixin;

import java.sql.SQLException;
import java.util.List;
import java.util.Map;

import org.springframework.orm.ibatis.SqlMapClientTemplate;

import com.google.common.collect.Maps;
import com.ihunanren.common.Pair;
import com.ihunanren.dao.WeixinMsgPojo;
import com.ihunanren.dao.WeixinMsgType;
import com.ihunanren.dao.weixin.IWeixinMsgDao;

/**
 * @author kalman03
 */
public class WeixinMsgDao extends SqlMapClientTemplate implements IWeixinMsgDao {

	@Override
	public List<?> getWeiXinMsg(long id, int currPage, int pageNum,WeixinMsgType msgType) throws SQLException {
		Map<String, String> map = getParameters(id, currPage, pageNum);
		String sqlMapping = "";
		switch (msgType) {
		case TEXT:
			sqlMapping = "WeixinMsgDao.getTextMsg";
			break;
		case IMAGE:
			sqlMapping = "WeixinMsgDao.getWeixinImageMsg";
			break;
		case LOCATION:
			sqlMapping = "WeixinMsgDao.getWeixinLocationMsg";
			break;
		case LINK:
			sqlMapping = "WeixinMsgDao.getWeixinLinkMsg";
			break;
		case EVENT:
			sqlMapping = "WeixinMsgDao.getWeixinEventMsg";
			break;
		default:
			break;
		}
		return getSqlMapClient().queryForList(sqlMapping, map);
	}
	
	private Map<String, String> getParameters(long id, int currPage, int pageNum) {
		Map<String, String> map = Maps.newHashMap();
		map.put("id",id+"");
		map.put("start", currPage+"");
		map.put("size",pageNum+"");
		return map;
	}
	
	@Override
	public int getWeixinMsgCount(long id, WeixinMsgType msgType) throws SQLException {
		String sqlMapping = "";
		switch (msgType) {
		case TEXT:
			sqlMapping = "WeixinMsgDao.getWeixinTextMsgCount";
			break;
		case IMAGE:
			sqlMapping = "WeixinMsgDao.getWeixinImageMsgCount";
			break;
		case LOCATION:
			sqlMapping = "WeixinMsgDao.getWeixinLocationMsgCount";
			break;
		case LINK:
			sqlMapping = "WeixinMsgDao.getWeixinLinkMsgCount";
			break;
		case EVENT:
			sqlMapping = "WeixinMsgDao.getWeixinEventMsgCount";
			break;
		default:
			break;
		}
		Object obj = getSqlMapClient().queryForObject(sqlMapping, id);
		return obj != null ? Integer.parseInt(obj.toString()) : 0;
	}

	@Override
	public boolean insertWeixinNormalMsg(Pair<? extends WeixinMsgPojo,WeixinMsgType> pair) throws SQLException {
		WeixinMsgType msgType = pair.snd;
		String sqlMapping = "";
		switch (msgType) {
		case TEXT:
			sqlMapping = "WeixinMsgDao.insertWeixinTextMsg";
			break;
		case IMAGE:
			sqlMapping = "WeixinMsgDao.insertWeixinImageMsg";
			break;
		case LOCATION:
			sqlMapping = "WeixinMsgDao.insertWeixinLocationMsg";
			break;
		case LINK:
			sqlMapping = "WeixinMsgDao.insertWeixinLinkMsg";
			break;
		case EVENT:
			sqlMapping = "WeixinMsgDao.insertWeixinEnventMsg";
			break;
		default:
			break;
		}
		Object o = getSqlMapClient().insert(sqlMapping, pair.fst);
		return o != null;
//		if (WeixinMsgType.TEXT == msgType) {
//			return insertTextMsg((WeixinTextMsgDO) pair.fst);
//		} else if (WeixinMsgType.IMAGE == msgType) {
//			return insertImageMsg((WeixinImageMsgDO) pair.fst);
//		}else if (WeixinMsgType.LOCATION == msgType) {
//			return insertLocationMsg((WeixinLocationMsgDO) pair.fst);
//		}else if (WeixinMsgType.LINK == msgType) {
//			return insertLinkMsg((WeixinLinkMsgDO) pair.fst);
//		}
//		return false;
	}
	
//	private boolean insertTextMsg(WeixinTextMsgDO weixinTextMsgDO)throws SQLException {
//		Object o = getSqlMapClient().insert("", weixinTextMsgDO);
//		return o != null;
//	}
//	
//	private boolean insertImageMsg(WeixinImageMsgDO weixinImageMsgDO)throws SQLException {
//		Object o = getSqlMapClient().insert("", weixinImageMsgDO);
//		return o != null;
//	}
//	
//	private boolean insertLocationMsg(WeixinLocationMsgDO weixinLocationMsgDO)throws SQLException {
//		Object o = getSqlMapClient().insert("", weixinLocationMsgDO);
//		return o != null;
//	}
//	
//	private boolean insertLinkMsg(WeixinLinkMsgDO weixinLinkMsgDO)throws SQLException {
//		Object o = getSqlMapClient().insert("", weixinLinkMsgDO);
//		return o != null;
//	}

	
}
